/*
Counting Sundays
Problem 19
Published on 14 June 2002 at 06:00 pm [Server Time]
You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
*/

var linq = require('linq');
var start = new Date().getTime();
var BigNumber = require('./BigNumber'); // Example for including files
var Big = require('big.js');

var monthsLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function isLeapYear(year)
{
    return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

var solveTask = function () {

    var sundaysFirstOfTheMonth = 0;

    var month = 0;
    var dayOfTheWeek = 0;
    var dayOfMonth = 0;

    for (var year = 1900; year <= 2000; ) {
        var incrementMonth = false;

        if (year >= 1901 && year <= 2000) {
            if (dayOfTheWeek == 6 && dayOfMonth == 0) {
                sundaysFirstOfTheMonth++;
            }
        }

        dayOfTheWeek++;
        if (dayOfTheWeek > 6) {
            dayOfTheWeek = 0;
        }

        dayOfMonth++;
        if (month == 1) {
            if ((isLeapYear(year) && dayOfMonth > 28) || dayOfMonth > 27) {
                incrementMonth = true;
            }
        } else if (dayOfMonth >= monthsLength[month]) {
            incrementMonth = true;
        }

        if (incrementMonth == true) {
            dayOfMonth = 0;
            month++;
            if (month > 11) {
                month = 0;
                year++;
                console.log(year);
            }
        }
    }

    return sundaysFirstOfTheMonth;
};
    
var result = solveTask();

console.log("Result: ", result);

var end = new Date().getTime();
var time = end - start;

console.log("Time: ", time);

require('readline')
    .createInterface(process.stdin, process.stdout)
    .question("Press [Enter] to exit...", function(){
        process.exit();
});